package com.JJTZ.android.view;


import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.JJTZ.android.R;

import android.content.Intent;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.JJTZ.android.adapter.CategoryListAdapter;
import com.JJTZ.android.adapter.ImageAdapter;
import com.JJTZ.android.model.TopItem;
import com.JJTZ.android.util.CommonUtil;
import com.JJTZ.android.widget.HorizontalListView;

public class MainActivity extends MasterActivity {

	String TOP5 = "http://dev.phpv5.com/product/top5";
	String search = "http://dev.phpv5.com/product/search/msg/htc";
	
	Button btn_top_login;
	EditText et_searchbar;
	HorizontalListView ga_top5;
	ListView lv_category;
	
	ArrayList<TopItem> topItem = new ArrayList<TopItem>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		
		btn_top_login = (Button)findViewById(R.id.btn_top_login);
		et_searchbar = (EditText)findViewById(R.id.et_search);
		ga_top5 = (HorizontalListView)findViewById(R.id.ga_top5);
		lv_category = (ListView)findViewById(R.id.lv_category);
		
		btn_top_login.setOnClickListener(mTestListener);
		//et_searchbar.setOnKeyListener(mSearchListener);
		et_searchbar.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainActivity.this,SearchingActivity.class));
				
			}
		});
		CommonUtil cu = new CommonUtil();
		String jsonStr = cu.getContent(TOP5);
		try {
			JSONObject obj = new JSONObject(jsonStr);
			if (obj.getString("return_code").equals("0"))
			{
				JSONArray nameList = obj.getJSONArray("data");
				TopItem top;
				for (int i = 0; i < nameList.length(); i++) {
					JSONObject oj = nameList.getJSONObject(i);
					top = new TopItem();
					top.setpImg(oj.getString("pImgUrls"));
					top.setpPrice(oj.getString("pPrice"));
					top.setpUrl(oj.getString("pUrl"));
					topItem.add(top);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		ImageAdapter ia = new ImageAdapter(topItem, this);
		ga_top5.setAdapter(ia);
		ga_top5.setOnItemClickListener(itemListener);
		lv_category.setAdapter(new CategoryListAdapter(this));
	}
	
	private OnItemClickListener itemListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(id<-1){
				return;
			}
			int realPosition=(int)id;
			TopItem top = (TopItem) parent.getItemAtPosition(realPosition);

			Intent intent = new Intent(MainActivity.this,WebViewActivity.class);
			intent.putExtra("url", top.getpUrl());
			MainActivity.this.startActivity(intent);
		}
		
	};
	
	private OnClickListener mTestListener = new OnClickListener() {
		
		public void onClick(View v) {
			startActivity(new Intent(MainActivity.this,SearchedActivity.class));
		}
	};
	
	private OnKeyListener mSearchListener = new OnKeyListener() {
		
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			startActivity(new Intent(MainActivity.this,SearchingActivity.class));
			return false;
		}
	};
}
